$(function(){
    $("#miniapp_detial_header li").click(function(){
        var data = $(this).attr("data");
        if (data == "domain_set") {
            var appid = $("#appid").val();
            window.location.href = "/admin/miniapps/wxa_webview_domain/"+appid;
        } else {
            $("#miniapp_detial_header li").removeClass("active");
            $(this).addClass("active");
            $("#miniapp_base_info").hide();
            $("#miniapp_version").hide();
            $("#miniapp_corp_info").hide();
            $("#miniapp_domain_set").hide();
            $("#miniapp_"+data).show();
        }
    });

    $("#release_method_row li").click(function(){
        var data = $(this).attr("data");
        $("#release_method_row li").removeClass("active");
        $(this).addClass("active");
        $("#release_method").val(data);
    });

    $("#wxa_search_ul li").click(function(){
        var data = $(this).attr("data");
        $("#wxa_search_ul li").removeClass("active");
        $(this).addClass("active");
        $("#status").val(data);
    });
});

function filingProgress() {
    mviewToastr("load","加载中,请稍等");
    $.ajax({
        "url": "/admin/miniapp/miniapp_last",
        "type": "GET",
        "data": {},
        success: function(res){
            if (res.status == "success") {
                setTimeout(function() {
                    colsemviewToastr();
                    window.location.href = "/admin/miniapps/miniapp_detial/"+res.data.appid;
                },2000);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function miniappBindLink() {
    mviewToastr("load","正在跳转中");
    $.ajax({
        "url": "/admin/miniapps/miniapp_bind",
        "type": "GET",
        "data": {},
        success: function(res){
            colsemviewToastr();
            if (res.status == "success") {
                window.location.href = res.data.url;
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(err){
            colsemviewToastr();
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function upgradeMiniappVersion(template_id,last_template_id) {
    var status = $("#status").val();
    var appid = $("#appid").val();
    if (status == 1) {
        if (template_id <= last_template_id) {
            mviewToastr("warning","当前已是最终版本,无需升级!");
        } else {
            window.location.href = "/admin/miniapps/weapp_audit/"+appid;
        }
    } else {
        mviewToastr("warning","当前小程序暂未通过腾讯审核,无法升级!");
    }
}

function getMiniappQrcode() {
    var status = $("#status").val();
    if (status != 1 && status != 8) {
        mviewToastr("warning","小程序未通过审核,暂时无法查看小程序码!");
        return;
    }
    var appid = $("#appid").val();
    if (appid == "") {
        mviewToastr("warning","小程序未通过审核,暂时无法查看小程序码!");
        return;
    }
    window.location.href = "/admin/miniapps/miniapp_wxaqrcode/"+appid;
}

function updateMiniapp(appid,miniapp_type) {
    if (miniapp_type == 1) {
        window.location.href = "/admin/miniapp/minapp_update/"+appid;
        // mviewToastr("warning","后台上架的微信小程序无法(无需)更新!");
    } else if (miniapp_type == 2) {
        window.location.href = "/admin/miniapp/minapp_update/"+appid;
    } else if (miniapp_type == 3) {
        window.location.href = "/admin/miniapp/minapp_update/"+appid;
    } else {
        window.location.href = "/admin/miniapp/minapp_update/"+appid;
        // mviewToastr("warning","扫码授权的微信小程序无法(无需)更新!");
    }
}

function deleteMiniapp(appid,miniapp_type) {
    if (miniapp_type == 1) {
        mviewToastr("warning","后台上架的微信小程序无法(无需)更新!");
    } else if (miniapp_type == 2) {
        window.location.href = "/admin/miniapp/minapp_update/"+appid;
    } else if (miniapp_type == 3) {
        window.location.href = "/admin/miniapp/minapp_update/"+appid;
    } else {
        mviewToastr("warning","扫码授权的微信小程序无法(无需)更新!");
    }
}

function confirmMviewNotification() {
    var mviewNotificationData = $("#mviewNotificationData").val();
    var notificationData = JSON.parse(mviewNotificationData);
    if (notificationData.type == "mini_bind") {
        window.location.href = "/admin/miniapp/minapp_add";
    } else if (notificationData.type == "miniapp_audit_cancel") {
        window.location.href = "/admin/miniapps/miniapp_audit_cancel/"+notificationData.appid;
    } else if (notificationData.type == "mini_replace") {
        registerMiniappBind();
    } else if (notificationData.type == "examine_pass") {
        examinePass();
    } else if (notificationData.type == "minapp_delete") {
        miniappDelete(notificationData.appid);
    } else if (notificationData.type == "mini_publish") {
        minappPublishSubmit();
    } else if (notificationData.type == "mini_delete") {
        window.location.href = "/admin/miniapps/miniapp_delete/"+notificationData.id;
    } else if (notificationData.type == "minapp_check") {
        miniappCheckScan(notificationData.appid,notificationData.visit_status);
    } else if (notificationData.type == "verify") {
        miniappVerify(notificationData.appid);
    } else if (notificationData.type == "verify_mini") {
        window.location.href = "/admin/miniapps/wxaauth/"+notificationData.appid;
    }
    colseMviewNotification();
}

function miniappDelete(appid) {
    $.ajax({
        "url": "/admin/miniapp/minapp_delete",
        "type": "GET",
        "data": {"appid":appid},
        success: function(res){
            if (res.status == "success") {
                $("#miniapp_"+appid).remove();
                mviewToastr("success",res.msg);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function miniappVerify(appid) {
    $.ajax({
        "url": "/admin/miniapps/miniapps_order",
        "type": "GET",
        "data": {"appid":appid},
        success: function(res){
            if (res.status == "success") {
                mviewToastr("success",res.msg);
                window.location.href = "/admin/miniapps/wxaauth/"+appid;
            } else {
                if (res.code == 1002) {
                    mviewToastr("error",res.msg,3);
                    setTimeout(function() {
                        window.location.href = "/admin/order/pay?corp_type="+res.data.corp_type;
                    },3000);
                } else {
                    mviewToastr("error",res.msg,5);
                }
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function miniappAddStatusChange() {
    var status = $("#status").val();
    if (status == 1) {
        $("#type_group").hide();
        $("#users_id").show();
    } else {
        $("#type_group").show();
        $("#users_id").hide();
    }
}

function miniappAddSubmit() {
    var name = $("#name").val();
    var appid = $("#appid").val();
    var secret = $("#secret").val();
    var original_id = $("#original_id").val();
    var status = $("#status").val();
    var type = $("#type").val();
    var users_id = $("#users_id").val();
    var disable = $("#disable").val();
    var scene = $("#scene").val();
    if (name.length <= 1) {
        mviewToastr("error","请输入2个字以上小程序名称!"); 
        return;
    }
    if (appid.length != 18) {
        mviewToastr("error","请输入正确的AppId!"); 
        return;
    }
    if (secret.length != 32) {
        mviewToastr("error","请输入正确的AppSecret!"); 
        return;
    }
    if (original_id.length != 15) {
        mviewToastr("error","请输入正确的小程序原始ID!"); 
        return;
    }

    $.ajax({
        "url": "/admin/miniapp/minapp_add_submit",
        "type": "GET",
        "data": {"name":name,"appid":appid,"secret":secret,"original_id":original_id,"status":status,"type":type,"users_id":users_id,"disable":disable,"scene":scene},
        success: function(res){
            if (res.status == "success") {
                mviewToastr("success","添加成功,3秒后返回列表!"); 
                setTimeout(function() {
                    window.location.href = "/admin/miniapp/miniapp_list";
                },3000);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function registerMiniappBind() {
    $.ajax({
        "url": "/admin/miniapps/miniapp_bind",
        "type": "GET",
        "data": {},
        success: function(res){
            if (res.status == "success") {
                window.location.href = res.data.url;
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function miniappBindScan() {
    mviewToastr("load","加载中,请稍等..."); 
    $.ajax({
        "url": "/admin/miniapp/miniapp_bind_scan",
        "type": "GET",
        "data": {},
        success: function(res){
            if (res.status == "success") {
                window.location.href = res.data.url;
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function miniappCheckScan(appid,visit_status) {
    mviewToastr("load","加载中,请稍等..."); 
    $.ajax({
        "url": "/admin/miniapp/miniapp_check_bind",
        "type": "GET",
        "data": {"appid":appid,"visit_status":visit_status},
        success: function(res){
            if (res.status == "success") {
                if (visit_status == 1) {
                    if (res.data.url != "") {
                        window.location.href = res.data.url;
                    } else {
                        mviewToastr("success",res.msg);
                        setTimeout(function(){
                            location.reload();
                        },2000);
                    }
                } else {
                    mviewToastr("success",res.msg);
                    setTimeout(function(){
                        location.reload();
                    },2000);
                }
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function fastRegisterCropWeapp() {
    var mini_name = $("#mini_name").val();
    var id = $("#id").val();
    if (mini_name.length <= 1) {
        mviewToastr("error","请输入2个及以上小程序名称!"); 
        return;
    }
    if (id == "") {
        mviewToastr("error","请选择小程序注册企业模板!"); 
        return;
    }
    $.ajax({
        "url": "/admin/miniapps/miniapp_register_corp_submit",
        "type": "GET",
        "data": {"mini_name":mini_name,"id":id},
        success: function(res){
            if (res.status == "success") {
                mviewToastr("success",res.msg); 
                setTimeout(function() {
                    window.location.href = "/admin/miniapps/miniapp_lists";
                },3000);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function jsUploadCallBack(id,imageurl,imageName,updata,imageid) {
    if (id == "miniapp_image") {
        $(".mini-edit-images").append('<img id="upload_back_'+imageid+'" src="'+imageurl+'">');
        $("#default_1896754").remove();
        $("#default_5610661").remove();
        $("#default_3876027").remove();
        $("#upload_back_1896754").remove();
        $("#upload_back_5610661").remove();
        $("#upload_back_3876027").remove();
    }
}

function jsImagesListsDeleteCallback(id,image,imageid) {
    if (id == "miniapp_image") {
        $("#upload_back_"+imageid).remove();
    }
}

function renderCategories() {
    $.each(categories,function(key,categorie) {
        if (categorie.father == 0) {
            $("#categorie").append('<option value="'+categorie.id+'">'+categorie.name+'</option>');
        }
    })
}

function categorieChange() {
    $("#categorie_chlid_row").show();
    var categorie_id = $("#categorie").val();
    $("#categorie_chlid").html("");
    $.each(categories,function(key,categorie) {
        if (categorie.father == categorie_id) {
            $("#categorie_chlid").append('<option value="'+categorie.id+'">'+categorie.name+'</option>');
        }
    })
}

function fastRegisterCropWeappEmpty() {
    $("#mini_name").val("");
    $("#name").val("");
    $("#code").val("");
    $("#code_type").val(1);
    $("#legal_persona_wechat").val("");
    $("#legal_persona_name").val("");
}

function miniappUpdateStatusChange() {
    var status = $("#status").val();
    if (status == 1) {
        $("#type_group").hide();
        $("#users_id").show();
    } else {
        $("#type_group").show();
        $("#users_id").hide();
    }
}

function minappPublishSubmit() {
    var nickname = $("#nickname").val();
    var appid = $("#appid").val();
    var signature = $("#signature").val();
    var media_id = $("#media_id").val();
    var head_image_url = $("#head_image_url").val();
    var users_id = $("#users_id").val();
    var license = $("#license").val();
    var naming_other_stuff = $("#naming_other_stuff").val();
    var categorie = $("#categorie").val();
    var categorie_chlid = $("#categorie_chlid").val();
    if (nickname.length <= 2) {
        mviewToastr("error","请输入2个字以上小程序名称!"); 
        return;
    }
    if (signature.length <= 9) {
        mviewToastr("error","请输入10字以上小程序简介!"); 
        return;
    }
    if (media_id == "" && head_image_url == "http://mmbiz.qpic.cn/mmbiz/a5icZrUmbV8p5jb6RZ8aYfjfS2AVle8URwBt8QIu6XbGewB9wiaWYWkPwq4R7pfdsFibuLkic16UcxDSNYtB8HnC1Q/0") {
        mviewToastr("error","请上传小程序头像!"); 
        return;
    }
    if (categorie == "") {
        mviewToastr("error","请选择微信小程序类目!"); 
        return;
    }
    mviewToastr("load","提交中,请稍等...",20); 
    $.ajax({
        "url": "/admin/miniapps/miniapp_publish_submit",
        "type": "GET",
        "data": {"nickname":nickname,"appid":appid,"signature":signature,"media_id":media_id,"head_image_url":head_image_url,"license":license,"naming_other_stuff":naming_other_stuff,"categorie":categorie,"categorie_chlid":categorie_chlid},
        success: function(res){
            colsemviewToastr();
            if (res.status == "success") {
                mviewToastr("load","提交审核中,请耐心等待...",20); 
                setTimeout(function() {
                    examinePass();
                },10000);
            } else {
                // 需要补充营业执照
                if (res.code == 91009) {
                    $("#license_row").show();
                    $("#naming_other_stuff_row").show();
                } else if (res.code == 1007) {
                    $("#license_row").show();
                    $("#naming_other_stuff_row").show();
                    $("#naming_other_stuff_row label").html('<b>*</b>资质或资质授权文件:');
                }
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function wxaRelease() {
    var appid = $("#appid").val();
    var status = $("#status").val();
    if (status == 1) {
        mviewToastr("error","小程序已发布,无需重复发布!"); 
        return;
    }
    $.ajax({
        "url": "/admin/miniapps/wxa_release/"+appid,
        "type": "GET",
        "data": {},
        success: function(res){
            if (res.status == "success") {
                mviewToastr("success",res.msg); 
                setTimeout(function() {
                    window.location.href = "/admin/miniapps/miniapp_detial/"+appid;
                },3000);
            } else {
                mviewToastr("error",res.msg);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function examinePass() {
    var appid = $("#appid").val();
    var template_id = $("#template_id").val();
    $.ajax({
        "url": "/admin/miniapps/wxa_commit/"+appid,
        "type": "GET",
        "data": {"template_id":template_id},
        success: function(res){
            if (res.status == "success") {
                $("#examine_pass").hide();
                mviewToastr("success",res.msg);
                setTimeout(function() {
                    window.location.href = "/admin/miniapps/miniapp_detial/"+appid;
                },3000);
            } else {
                mviewToastr("error",res.msg);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function wxaUpgradation() {
    var appid = $("#appid").val();
    var old_template_id = parseInt($("#old_template_id").val());
    var template_id = parseInt($("#template_id").val());
    if (old_template_id >= template_id) {
        mviewToastr("warning","当前已是最新版本,无需升级!"); 
        return;
    }
    examinePass();
}

function getName() {
    var names = ["朗杭","启明","洁通","伟海","杰渝","超西","谦汇","秋涛","先众","正扬","志特","桦钧","新广","云浩","杭纶","宏平","岳源","风河","兴联","祥骏","华烽","青齐","禾成","弘平","言晟","元百","成杰","东诚","恩伦","熙智","博勤","虹广","欧智","恒永","丰宁","佳同","海联","海洁","才森","立若","林鼎","乐振","建聪","圣云","伦熙","泽顺","金青","杉德","兴安","畅博","久霖","鑫仁","迅雨","智昌","裕辰","敬楠","伦君","道威","言佑","尊海","卓亿","诚振","杰洋","诚新","仲盈","凯众","楠途","鸣晏","希渝","泰盛","辉桦","帝豪","金渝","立晟","纶贤","瀚杰","鑫业","鼎财","利隆","广达","昂航","卓翰","聪志","智嘉","畅腾","亿熙","旭江","建博","毅联","西安","弘秋","金泰","奇致","博亚","汇亚","景京","盛尚","冠智","立德","嘉迅","业泽","安弘","善隆","言谦","雄卓","平琛","昂信","吉青","优桦","弘烽","诚杭","佳东","明仲","洋仁","钧贤","澄岩","泰道","仁华","铭信","隆启","力业","卓文","蓝峰","鸣东","同振","宇卫","仁宁","炫炫","滨毅","森元","东田","盈鑫","吉岳","青晟","秋伊","卓才","衡智","诺源","峰华","航纳","辰柏","皓森","仟健","亮元","炫扬","元勤","智扬","成良","雷卓","奕超","熙虹","楚岩","霖言","海意","江鸣","秋盛"];
    var randomNumber1 = Math.floor(Math.random() * 155) + 1;
    var randomNumber2 = Math.floor(Math.random() * 7) + 1;
    var cnames = ["工具","助手","小助手","查询助手","小工具","查询系统","查询工具","活动助手"];
    $("#nickname").val(names[randomNumber1]+cnames[randomNumber2]);
}

function getHeadImg() {
    var imageLists = ["https://res.wailian1.cn/miniapp/links/20231128/b3c4896eae48441c2d0b4a9d3d130b85.jpg","https://res.wailian1.cn/miniapp/links/20231128/57d45b6bbf1ee57c0f234288a8cfa685.jpg","https://res.wailian1.cn/miniapp/links/20231128/fd0d320b5f604ced1697fc1de04ef924.jpg","https://res.wailian1.cn/miniapp/links/20231128/dad50de3654612791c774878eb2a7148.jpg","https://res.wailian1.cn/miniapp/links/20231128/ea5cdbb1edafb8897c83533c67b6df19.jpg","https://res.wailian1.cn/miniapp/links/20231128/1bd9574e82fd79865f6ab5ab8f8bd4f7.jpg","https://res.wailian1.cn/miniapp/links/20231128/395185d2edada699687274410f90ac59.jpg","https://res.wailian1.cn/miniapp/links/20231128/f7767f22ea65d539d897f4ce6f0a6519.jpg","https://res.wailian1.cn/miniapp/links/20231128/b1ce7b36628842617c19e32ff766d5fc.jpg","https://res.wailian1.cn/miniapp/links/20231128/4ef65af68f578d798ced017a7ff0d4c4.jpg","https://res.wailian1.cn/miniapp/links/20231128/10b1719eabb3d8ec1157204c1bdf9f42.jpg","https://res.wailian1.cn/miniapp/links/20231128/df6129c7a3707f86590473f54b5933fe.jpg","https://res.wailian1.cn/miniapp/links/20231128/5ce7a76786b80cfe2d71f4d0ba693715.jpg","https://res.wailian1.cn/miniapp/links/20231128/fcfda8c3b249efec699bfccc3cc71fa2.jpg","https://res.wailian1.cn/miniapp/links/20231128/3e1d674ecad393fee79654cdb8480e02.jpg","https://res.wailian1.cn/miniapp/links/20231128/de7cf907e5a1d1a7a32fba3ad240dddd.jpg","https://res.wailian1.cn/miniapp/links/20231128/ac571c8f68a82f308a5dfed2b39d20b1.jpg","https://res.wailian1.cn/miniapp/links/20231128/a35a498795a9eade659cdccc63587aba.jpg","https://res.wailian1.cn/miniapp/links/20231128/738bcedb5a8939981e43bbc72714d220.jpg","https://res.wailian1.cn/miniapp/links/20231128/6957333e05cd5f900509e62e8a267bf7.jpg","https://res.wailian1.cn/miniapp/links/20231128/2664cb138a232e71a9a1c5b33b3a2ff3.jpg","https://res.wailian1.cn/miniapp/links/20231128/41596d77092970499518a479e5f4407b.jpg","https://res.wailian1.cn/miniapp/links/20231128/2654b564939e529ea3c4ca3fba6611f0.jpg","https://res.wailian1.cn/miniapp/links/20231128/f4b6f014371416f636bdf984615db3d0.jpg","https://res.wailian1.cn/miniapp/links/20231128/e074fc145e3f2806b43951a26cb2e794.jpg"];
    var randomNumber1 = Math.floor(Math.random() * 25);
    var head_img = imageLists[randomNumber1];
    $("#media_id_show").attr("src",head_img);
    $("#media_id").val(head_img);
}